package com.ihr360.job.core.scope.context;

import org.springframework.core.AttributeAccessorSupport;

import java.util.Arrays;

/**
 * Context object for weakly typed data stored for the duration of a chunk
 * (usually a group of items processed together in a transaction). If there is a
 * rollback and the chunk is retried the same context will be associated with
 * it.
 *
 * @author Dave Syer
 *
 */
public class ChunkContext extends AttributeAccessorSupport {

    private final StepContext stepContext;

    private boolean complete = false;

    /**
     * @param stepContext the current step context
     */
    public ChunkContext(StepContext stepContext) {
        this.stepContext = stepContext;
    }

    /**
     * @return the current step context
     */
    public StepContext getStepContext() {
        return stepContext;
    }

    /**
     * @return true if there is no more processing to be done on this chunk
     */
    public boolean isComplete() {
        return complete;
    }

    /**
     * Setter for the flag to signal complete processing of a chunk.
     */
    public void setComplete() {
        this.complete = true;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.format("ChunkContext: attributes=%s, complete=%b, stepContext=%s", Arrays
                .asList(attributeNames()), complete, stepContext);
    }

}